﻿
using MediatR;
using RBAC.Common.Domains;
using RBAC.Common.Dto;
using RBAC.Common.infrastructures.interfaces;
using RBAC.Enum;

namespace RBAC.api.Application.commond.RBAC
{
    public class USerRoleHtmlHanderL : IRequestHandler<USerRoleHtmlCommand, Rulst>
    {

        private readonly IbaseRpository<User> _user;
        private readonly IbaseRpository<Role> _role;
        private
            readonly IbaseRpository<userRole> _userRole;

        public USerRoleHtmlHanderL(IbaseRpository<User> user, IbaseRpository<Role> role, IbaseRpository<userRole> userRole)
        {
            _user = user;
            _role = role;
            _userRole = userRole;
        }
        /// <summary>
        /// 用户角色列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Rulst> Handle(USerRoleHtmlCommand request, CancellationToken cancellationToken)
        {
            
            var query=new List<USerRoleGEt>() {};    
           


            var list=new Rulst() {Code=(int)Reult .success};



            query = (from a in _user.Query()
                              join b in _userRole.Query() on a.Userid equals b.Userid
                              select new USerRoleGEt()
                              {
                                  Accounts = new List<Account>(),
                                  Roleid=a.Userid,
                                  username=a.UserName
                              }).ToList();



            var PUTGEt = query;

            foreach (var item in PUTGEt) {


                item.Accounts = (from a in _userRole.Query()
                                 join b in _role.Query() on a.Roleid equals b.Roleid
                                 where a.Userid == item.Roleid
                                 select new Account()
                                 {
                                     Rolename = b.RoleName,
                                 }).ToList();                     
            }


            query = PUTGEt;

            list.data = query;
            return list;


        }
    }
}
